package mof.gov.dmdc.view.searchadapter;

import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import mof.gov.dmdc.view.common.Common;

import oracle.jbo.ViewObject;

public class SearchAdapter {
    public SearchAdapter() {
        super();
        ViewObject vo = getViewObject();

        globalTotalSize = vo.getEstimatedRowCount();

    }

    private static String ITERATOR_NAME = "searchVOIterator";
    private static String META_ITERATOR_NAME = "searchDefVOIterator";
    protected int maxFletSize = 50;
    protected long currenTotalSize;
    protected long globalTotalSize = 0;


    public ViewObject getViewObject() {
        return Common.getViewObjectForIterator(ITERATOR_NAME);
    }

    public ViewObject getViewSearchField() {
        return Common.getViewObjectForIterator(META_ITERATOR_NAME);
    }

    public void setMaxFletSize(int maxFletSize) {
        this.maxFletSize = maxFletSize;
    }

    public int getMaxFletSize() {
        return maxFletSize;
    }

    public void setCurrenTotalSize(long currenTotalSize) {
        this.currenTotalSize = currenTotalSize;
    }

    public long getCurrenTotalSize() {
        return currenTotalSize;
    }

    public void setGlobalTotalSize(long globalTotalSize) {
        this.globalTotalSize = globalTotalSize;
    }

    public long getGlobalTotalSize() {
        return globalTotalSize;
    }

    public void valueChangeListenner(ValueChangeEvent valueChangeEvent) {
        int newMaxFetchSize = Integer.parseInt(valueChangeEvent.getNewValue().toString());
        if (maxFletSize != newMaxFetchSize) {
            ViewObject vo = getViewObject();
            vo.setMaxFetchSize(newMaxFetchSize);
            vo.clearCache();
        }
    }

    protected void refreshPage() {
        FacesContext fc = FacesContext.getCurrentInstance();
        String refreshpage = fc.getViewRoot().getViewId();
        ViewHandler ViewH = fc.getApplication().getViewHandler();
        UIViewRoot UIV = ViewH.createView(fc, refreshpage);
        UIV.setViewId(refreshpage);
        fc.setViewRoot(UIV);
    }
}

